package com.tosit.bmx.service.admin.Impl;

import com.tosit.bmx.domian.*;
import com.tosit.bmx.mapper.*;
import com.tosit.bmx.service.admin.CarManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 车型，科目，题型，套题管理类
 *
 * @author tsl
 * @create 2018-04-24 14:19
 **/
@Service
public class CarManagerServiceImpl implements CarManagerService {
    @Autowired
    CartypeMapper cartypeMapper;
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    ExertypeMapper exertypeMapper;
    @Autowired
    SecondTypeMapper secondTypeMapper;

    @Autowired
    SubjectMapper subjectMapper;

    @Autowired
    SecondSubMapper secondSubMapper;

    @Override
    @Transactional
    public Integer addCartype(Cartype cartype) {
        return cartypeMapper.insertSelective(cartype);
    }

    @Override
    @Transactional
    public Integer removeCartype(Integer id) {
        return cartypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public Integer modifyCartype(Cartype cartype) {
        return cartypeMapper.updateByPrimaryKeySelective(cartype);
    }

    @Override
    @Transactional
    public Integer addCourse(Course course) {
        return courseMapper.insertSelective(course);
    }

    @Override
    @Transactional
    public Integer removeCourse(Integer id) {
        return courseMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public Integer modifyCourse(Course course) {
        return courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    @Transactional
    public Integer addExertype(Exertype exertype) {
        return exertypeMapper.insertSelective(exertype);
    }

    @Override
    @Transactional
    public Integer removeExertype(Integer id) {
        return exertypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public Integer modifyExertype(Exertype exertype) {
        return exertypeMapper.updateByPrimaryKeySelective(exertype);
    }


    @Override
    @Transactional
    public Integer addSubject(Subject subject) {
        return subjectMapper.insertSelective(subject);
    }


    @Override
    @Transactional
    public Integer removeSubject(Integer id) {
        return subjectMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public Integer modifySubject(Subject subject) {
        return subjectMapper.updateByPrimaryKeySelective(subject);
    }

    @Override
    @Transactional
    public Integer addSecondType(SecondType secondType) {
        return secondTypeMapper.insertSelective(secondType);
    }

    @Override
    @Transactional
    public Integer removeSecondType(Integer id) {
        secondSubMapper.deleteBySecondType(id);
        return secondTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public Integer modifySecondType(SecondType secondType) {
        return secondTypeMapper.updateByPrimaryKeySelective(secondType);
    }
    /**
     * 删除一个或多个套题
     *
     * @param ids
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    @Transactional
    public Integer removeSecondTypes(List<Integer> ids) {
        secondSubMapper.deleteBySecondTypes(ids);
        return secondTypeMapper.deleteSeconds(ids);
    }
    /**
     *  复制一个或多个套题到另一个或几个题型里
     * @author tsl
     * @date 2018/4/25 9:40
     * @param secondTypes list(id(复制套题的id),name,exid,imgpath,px)
     * @return
     */
    @Override
    @Transactional
    public Integer copySecondTypes(List<SecondType> secondTypes) {
        Integer res=null;
        for (int i = 0; i < secondTypes.size(); i++) {
            SecondType secondType = secondTypes.get(i);
            Integer coypId=secondType.getId();
            System.out.println(coypId);
            secondType.setId(null);
            secondTypeMapper.insertSelective(secondType);
            Integer insertid = secondType.getId();
            res = secondSubMapper.copySubIdBySecondTypeId(insertid,coypId);
            secondType.setId(insertid);
            if (secondType.getPx() == null) {
                secondType.setPx(1);
            }
            if (secondType.getZname() == null) {
                secondType.setZname("");
            }
        }
        return res;
    }
    /**
     * 根据套题id查找套题
     *
     * @param seid
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    public List<SubjectVo> getSubjectBySeId(Integer seid) {
        return subjectMapper.selectBySeId(seid);
    }
    /**
     * 删除一个或多个套题和题的关联
     * @author tsl
     * @date 2018/4/25 14:56
     * @param ids 删除关联对象id
     * @return
     */
    @Override
    @Transactional
    public Integer deleterelateSubs(List<Integer> ids) {
        return secondSubMapper.deleteRelate(ids);
    }
    /**
     * 添加一个或多个套题和题的关联
     * @author tsl
     * @date 2018/4/25 14:56
     * @param secondSubs 添加关联对象（seid，sid，sorder）集合
     * @return
     */
    @Override
    @Transactional
    public Integer addRelateSub(List<SecondSub> secondSubs) {
        return secondSubMapper.insertSelectives(secondSubs);
    }
    /**
     *
     * 修改一个或多个套题和题的关联的排序
     * @author tsl
     * @date 2018/4/25 14:56
     * @param secondSubs （id，sorder）
     * @return
     */
    @Override
    public Integer modifyRelateSubs(List<SecondSub> secondSubs) {
        return secondSubMapper.updatesSort(secondSubs);
    }

    /**
     * 查找所有题
     *
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    public List<Subject> getSubjects() {
        return subjectMapper.getAllSub();
    }
    /**
     *  复制一个或多个套题到另一个套题里
     * @author tsl
     * @date 2018/4/25 9:40
     * @param sencondSubVos SencondSubVo(copyId(复制套题的id)的集合,copyId(复制到某套题的id))
     * @return
     */
    @Override
    @Transactional
    public Integer copySecondTypesToSelf(SencondSubVo sencondSubVos) {
        List<SecondSub> result = new ArrayList<>();
        Integer lastSort = null;
        List<SecondSub> secondSubs1 = secondSubMapper.selectBySeid(sencondSubVos.getToCopyId());
        List<SecondSub> secondSubs= secondSubMapper.selectBySeids(sencondSubVos.getCopyId());
        lastSort = secondSubs1.get(secondSubs1.size() - 1).getSorder();
        if (secondSubs.removeAll(secondSubs1)){
            for (int i = 0; i < secondSubs.size(); i++) {
                SecondSub secondSub = secondSubs.get(i);
                secondSub.setSorder(++lastSort);
                result.add(secondSub);
            }
        }
        return secondSubMapper.insertSelectives(result);
    }
    /**
     * 根据题目id查找题目
     *
     * @param id
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    public Subject getSubjectById(Integer id) {
        return subjectMapper.selectByPrimaryKey(id);
    }
    /**
     * 根据题目id题目所属的所有父级
     *
     * @param id
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    public List<CartypeVoEnhance> getSubjectBySubId(Integer id) {
        return cartypeMapper.selectBySubId(id);
    }
    /**
     * 查找对应类型的所有题
     *@param  type 0单选 1判断 2多选
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    public List<Subject> getSubjectsByType(Integer type) {
        return subjectMapper.selectByType(type);
    }
    /**
     * 关键词或者ID查找题
     *@param  keyCode 查询条件
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    public List<Subject> seekSubjects(String keyCode) {
        return subjectMapper.seekByKeyCode(keyCode);
    }
    /**
     * 删除一个或多个题
     *
     * @param ids
     * @return
     * @author tsl
     * @date 2018/4/24 14:31
     */
    @Override
    public Integer removeSubjects(List<Integer> ids) {
        secondSubMapper.deleteBySubjectId(ids);
        return subjectMapper.deleteByids(ids);
    }


}
